home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c++,comp.lang.c,comp.os.ms-windows.programmer.misc
- Subject: Re: fastest code
- Date: 14 Apr 1996 10:10:13 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4krbhlINNbk4@keats.ugrad.cs.ubc.ca>
- References: <316112A2.7D37@public.sta.net.cn> <4kjpn0INN817@keats.ugrad.cs.ubc.ca> <829352535snz@genesis.demon.co.uk> <4kof6e$te@news1.mnsinc.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4kof6e$te@news1.mnsinc.com>,
- Szu-Wen Huang <huang@mnsinc.com> wrote:
-
- >One would almost always expect addition to be faster than multiplication.
- >An optimizing compiler might be able to realize they are actually the
- >same, and perhaps even decide to implement both as bitwise shifts. In
- >any case, I wouldn't expect the former to be slower than the latter. (It
- >certainly *might* be, but chances are slim).
-
- I _would_ expect the latter to be faster. In fact when I first encountered the
- code, I thought to myself, ``why isn't this written as 2*i''?
-
- Why I would expect it to have a beter chance at being faster is that
- multiplication by a constant is a common subject of optimization. Adding a
- quantity to itself is not a common idiom, hence it's not an important
- optimization to implement. The gain by doing strength reduction from a
- mulitiplication to an addition is far more important than any gain in speed you
- could squeeze out of optimizing simple additions.
-
- If I were writing a compiler, I wouldn't bother ``optimizing'' this case at
- all: If the programmer thinks he knows better, let him have his addition!
-